import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

import { FsService } from './../../framework'
import { Controller, Get, Res, Req, Post, Body } from '@nestjs/common'
import { Request, Response } from 'express'
import { ApiExcludeEndpoint } from '@nestjs/swagger'
import { join } from 'path'
declare const MEEPO_ROOT: string
@Controller()
export class InstallController {
  private configPath: string
  constructor(private fs: FsService) {
    this.configPath = join(MEEPO_ROOT, 'data', 'config.json')
  }
  @Get()
  @ApiExcludeEndpoint()
  index(@Req() req: Request, @Res() res: Response) {
    res.render('index', { req })
  }

  @Get('test')
  test() {
    return MEEPO_ROOT
  }

  @Get('menu')
  menus() {
    return [
      {
        icon: '',
        text: '数据库',
        children: [
          {
            text: 'mysql',
            link: '/mysql'
          },
          {
            text: 'redis',
            link: '/redis'
          }
        ]
      }
    ]
  }

  @Post('config')
  @ApiExcludeEndpoint()
  config(@Body() body): Observable<any> {
    return this.fs.file_put_contents(this.configPath, body).pipe(
      map(res => {
        return {
          code: 0,
          msg: '保存成功'
        }
      })
    )
  }
}
